﻿using SmartPowerHub.Communication.Modbus;
using System;
using System.IO.Ports;
using System.Runtime.InteropServices;
using System.Threading.Tasks;

namespace SmartPowerHub.Test
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            #region Rtu

            IModbus modbusRtu = new ModbusRtuClient("COM6", endianFormat: EndianFormat.ABCD);
            await modbusRtu.ConnectAsync();
            //写入字符串
            var resultRtu = await modbusRtu.WriteString(0, "Holle Word", 0x01);
            if (resultRtu.State)
            {
                //读取字符串
                var readResultRtu = await modbusRtu.ReadString(FunctionCode.ReadHoldingRegisters, 0, 5, 0x01);
                Console.WriteLine($"读取字符串：{readResultRtu.Value}");
            }
            //写入float
            resultRtu = await modbusRtu.WriteFloat(0, 11.2f, 0x01);
            if (resultRtu.State)
            {
                //读取float
                var readResultRtu = await modbusRtu.Read<float>(FunctionCode.ReadHoldingRegisters, 0, 2, 0x01);
                Console.WriteLine($"读取float：{readResultRtu.Value}");
            }
            //写入多个s16
            List<short> shorts = new List<short>() { 3, 5, 7, 9 };
            resultRtu = await modbusRtu.WriteMulti<short>(0, shorts.ToArray(), 0x01);
            if (resultRtu.State)
            {
                //读取多个s16
                var readResultRtu = await modbusRtu.Read<short>(FunctionCode.ReadHoldingRegisters, 0, 4, 0x01);
                string data = string.Empty;
                foreach (var item in readResultRtu.ValueArray)
                {
                    data += item + " ";
                }
                Console.WriteLine($"读取多个s16：{data}");
            }
            modbusRtu.Dispose();

            #endregion

            #region Tcp
            //IModbus modbusTcp = new ModbusTcpClient("127.0.0.1", 502, endianFormat: EndianFormat.ABCD);
            //await modbusTcp.ConnectAsync();
            ////写入字符串
            //var resultTcp = await modbusTcp.WriteString(0, "Holle Word");
            //if (resultTcp.State)
            //{
            //    //读取字符串
            //    var readResultTcp = await modbusTcp.ReadString(FunctionCode.ReadHoldingRegisters, 0, 5);
            //    Console.WriteLine($"读取字符串：{readResultTcp.Value}");
            //}
            ////写入float
            //resultTcp = await modbusTcp.WriteFloat(0, 11.2f);
            //if (resultTcp.State)
            //{
            //    //读取float
            //    var readResultTcp = await modbusTcp.ReadFloat(FunctionCode.ReadHoldingRegisters, 0, 2);
            //    Console.WriteLine($"读取float：{readResultTcp.Value}");
            //}
            ////写入s16
            //resultTcp = await modbusTcp.WriteS16(0, 11);
            //if (resultTcp.State)
            //{
            //    //读取s16
            //    var readResultTcp = await modbusTcp.ReadS16(FunctionCode.ReadHoldingRegisters, 0, 1);
            //    string data = string.Empty;
            //    foreach (var item in readResultTcp.ValueArray)
            //    {
            //        data += item + " ";
            //    }
            //    Console.WriteLine($"读取单个s16：{data}");
            //}
            //List<short> shorts = new List<short>() { 3, 5, 7, 9 };
            //resultTcp = await modbusTcp.WriteMulti<short>(0, shorts.ToArray(), 0x01);
            //if (resultTcp.State)
            //{
            //    //读取多个s16
            //    var readResultTcp = await modbusTcp.ReadS16(FunctionCode.ReadHoldingRegisters, 0, 4, 0x01);
            //    string data = string.Empty;
            //    foreach (var item in readResultTcp.ValueArray)
            //    {
            //        data += item + " ";
            //    }
            //    Console.WriteLine($"读取多个s16：{data}");
            //}
            //modbusTcp.Dispose();
            #endregion

            Console.ReadKey();
        }
    }
}
